<template>
	<view class="content">
		<view class="code-contaner mywzcard">
			<div class="cardbox">
				<view class="time f-26">{{wzInfo.ctidNum}}</view>
				<view class="num f-26">{{wzInfo.validDate}}</view>
				<image class="card-img" src="/static/images/card_back.png"></image>
			</div>
			<view class="tip flexbox flexbox-center flexbox-middle">国家“互联网+”可信身份认证平台生成</view>
			<view class="hr-box flexbox flexbox-middle">
				<view class="circle"></view>
				<view class="hr"></view>
				<view class="circle"></view>
			</view>
			<image class="code-img" :src="wzCode"></image>
		</view>
		<!-- <view class="flexbox flexbox-middle flexbox-center">
			<image class="card-img" src="/static/images/small_logo.png"></image>
			<view></view>
		</view> -->
		<canvas canvas-id="myCanvas" class="hiddenCanvas" style="width:600rpx; height: 600px"></canvas>
	</view>

</template>


<script>
	var creatQR = require('../../static/qrcode/creatQR.js')
	export default {
		data() {
			return {
				ctid: '',
				randomNumber: '',
				qRCodeData: '',
				wzCode: '',
				user_id: '',
				cardDetail: {},
				times: 60,
				timer: null,
				wzInfo: {}
			}
		},
		onLoad: function(e) {
			uni.showLoading({
			    title: '请求中...'
			});
			let ctid = e.ctid
			if (ctid) {
				uni.setStorageSync('ctid', ctid);
			}
			this.$HTTP.post('/wechat/userinfo', {}, false, false, true).then((r) => {
				this.getCheckData(r.data)
				this.user_id = r.data.userId
			}).catch(err => {
				
			})
		},
		onShow() {
			/*if (!this.ctid) {
				uni.redirectTo({
				    url: `/pages/openWz-step1/index`
				});
				return false
			}*/
		},
		onHide() {
			if (this.timer) {
				clearInterval(this.timer)
			}
		},
		methods: {
			getCheckData(response) {
				let that = this
				this.ctid = uni.getStorageSync('ctid');
				if (!this.ctid) {
					if(response.role == 'user' ) {
						uni.redirectTo({
						    url: `/pages/openWz-step1/index?type=wzcard`
						});
						return false
					} else {
						uni.showModal({
						    title: '提示',
						    content: '网证已失效，需重新认证',
							confirmColor: '#3388FF',
						    success: function (res) {
						        if (res.confirm) {
						            uni.redirectTo({
						                url: `/pages/openWz-step1/index?type=wzcard`
						            });
						        } else if (res.cancel) {
									uni.navigateBack({
									    delta: 1
									});
						            console.log('用户点击取消');
						        }
						    }
						});
					}
					uni.hideLoading();
					return false
				}
				this.wzInfo = this.$WZAPP.getCtidNum(this.ctid).value;
				let reqCodeData = new this.$WZAPP.ReqCodeData('0013', '00001105', this.ctid);
				let appdata = new this.$WZAPP.ApplyData('0013', '00001105', 0);
				let appdataResult = this.$WZAPP.getApplydata(appdata).value
				this.$HTTP.post('/ctid/unity', {
					type: this.$BSNCODE.CREATE_CODE_REQUEST,
					applyData: appdataResult
				}, false, false, true).then((res2) => {
					this.randomNumber = res2.data.randomNumber
					// console.log(this.$WZAPP.getReqQRCodeData(this.randomNumber, reqCodeData), 4444)
					this.qRCodeData = this.$WZAPP.getReqQRCodeData(this.randomNumber, reqCodeData).value
					
					this.getCode(this.qRCodeData, res2.data.bsn)
				}).catch(err => {

				})

			},
			
			reload() {
				uni.showLoading({
				    title: '请求中...'
				});
				if (this.timer) {
					clearInterval(this.timer)
				}
				this.getCheckData()
			},

			getCode(checkData, bsn) {
				let storeData = {
					user_id: this.user_id
				}
				let param = {
					checkData: checkData,
					storeData: JSON.stringify(storeData),
					bsn: bsn
				}
				this.$HTTP.post('/ctid/createCode', param, false, false, true).then((res) => {
					//console.log(res, 999)
					this.creatQR(res.data)
				}).catch(err => {
					if(err.code == '220005') {
						uni.showModal({
						    title: '提示',
						    content: '网证已失效，需重新认证',
							confirmColor: '#3388FF',
						    success: function (res) {
						        if (res.confirm) {
						            uni.redirectTo({
						                url: `/pages/openWz-step1/index?type=wzcard`
						            });
						        } else if (res.cancel) {
									uni.navigateBack({
									    delta: 1
									});
						            console.log('用户点击取消');
						        }
						    }
						});
					}
				})
			},
			creatQR(res) {
				let that = this
				let ctx = wx.createCanvasContext('myCanvas')
				creatQR.drawCertQrCode({
						ctx: ctx,
						codeContent: res.imgStream,
						setWidth: parseInt(res.width)
					},
					1,
					function(canvasWidth) {
						setTimeout(() => {
							uni.canvasToTempFilePath({
							  x: 0,
							  y: 0,
							  width: canvasWidth,
							  height: canvasWidth,
							  destWidth: canvasWidth,
							  destHeight: canvasWidth,
							  canvasId: 'myCanvas',
							  success: function(res) {
								  that.wzCode = res.tempFilePath
								  /*that.times = 60
								  that.timer = setInterval(() => {
								  	that.times --
								  	if (that.times <= 0 ){
								  		that.times = 0
								  		clearInterval(that.timer)
								  	}
								  }, 1000)*/
								  uni.hideLoading();
							  } ,
							  fail(err) {
								console.log(err)
								reject(err)
							  }
							})
						}, 1000)
					}
				)
			}
		}


	}
</script>
<style lang="scss" scoped>
	@import "./index.scss";
</style>
<style lang="scss">
	page{
		background-color: #f8f8f8;
	}
</style>